<?php
namespace Shopadmin\Controller;
use Think\Controller;
class PageController extends ExtendController {
	public function customs(){
		Vendor('Page');
		Vendor("taobaosdk.top.request.VasSubscSearchRequest");
		Vendor("taobaosdk.top.request.VasOrderSearchRequest");
		$code = I("code")?I("code"):"ts-30901";
		// 订单记录查询起始时间
		$ISV = D("IsvView");
		$total = $ISV->where("isv.article_code='".$code."'")->count("isv.id");
		$Page = new \Page($total,$this->listRows);
		$Page->rollPage=$this->rollPage;
		$show = $Page->show();
		$data = $ISV->where("isv.article_code='".$code."'")->limit($Page->firstRow.','.$Page->listRows)->order("isv.id DESC")->getField("id,article_code,item_code,article_item_name,article_name,biz_type,order_cycle_end,fee,create_time,order_id,nick,order_cycle,biz_order_id,prom_fee,refund_fee,order_cycle_start,total_pay_fee,sid,curtool,front_pic_url,extend");
		foreach($data as $k=>$v){
			$data[$k]["nick_encode"]=urlencode($data[$k]["nick"]);
		}
		$this->assign('totalpg',(int)$Page->totalPages);
		$this->assign('curpg',$Page->nowPage);
		$this->assign('current',"page");
		$this->assign('page',$show);
		$this->assign('data',$data);
		$this->display();
	}
	public function save_isv_extend(){
		$data["extend"] = I("beizhu");
		$sid = I("sid");
		$ISV = M("IsvOrders");
		$re = $ISV->where("`sid`=".$sid)->save($data);
		$this->ajaxReturn($re);
	}
	public function loadorders(){
		Vendor("taobaosdk.top.request.VasOrderSearchRequest");
		Vendor("taobaosdk.top.request.ShopGetRequest");
		$code = I("code");//订单抽大奖
		$start = I("start");//订单抽大奖
		$end = I("end");//订单抽大奖
		if($code){
				$req = new \VasOrderSearchRequest;
				$req->setArticleCode($code);
				$req->setStartCreated(date("Y-m-d 00:00:00",strtotime($start)));//昨天0天开始
				$req->setEndCreated(date("Y-m-d 23:59:59",strtotime($end)));//昨天一整天
				$req->setPageSize(40);
				$req->setPageNo(1);
				$resp = $this->c->execute($req);
				$out[]=$resp;
			foreach($resp->article_biz_orders->article_biz_order as $order){
				$order=(array)$order;
				$shop = new \ShopGetRequest;
				$shop->setFields("sid");
				$shop->setNick($order["nick"]);
				$shopresp = $this->c->execute($shop);
				if($shopresp){
					$order["sid"]=$shopresp->shop->sid;
					$ISV = M("IsvOrders");
					$ISV->add($order);
					$out[]=$ISV->_sql();
				}
			}
			for($i=2;$i<=ceil((int)$resp->total_item/40);$i++){
				// 再从第二页开始继续取
				$req->setPageNo($i);
				$resp = $this->c->execute($req);
				$out[]=$resp;
				foreach($resp->article_biz_orders->article_biz_order as $order){
					$order=(array)$order;
					$shop = new \ShopGetRequest;
					$shop->setFields("sid");
					$shop->setNick($order["nick"]);
					$shopresp = $this->c->execute($shop);
					if($shopresp){
						$order["sid"]=$shopresp->shop->sid;
						$ISV = M("IsvOrders");
						$ISV->add($order);
						$out[]=$ISV->_sql();
					}
				}
			}
		}
		$this->assign("start",$start);
		$this->assign("end",$end);
		$this->assign("out",$out);
		$this->assign("title","手动导入isv订单");
		$this->display();
	}
	public function index(){
		$PG = M("Page");
		$data = $PG->getField("id,title,content,edit");
		$this->assign("data",$data);
		$this->display();
		$this->assign("current","page");
	}
	public function edit(){
		$add = I("get.add");
		$save = I("get.save");
		$id = (int)I("get.id");
		$content = I("post.content");
		$title = I("post.title");
		$PG = M("Page");
		if($id){
			$cre = current($PG->where("`id`=".$id)->getField("id,content,title"));
			$this->assign("id",$id);
			$this->assign("edit",true);
			$this->assign("content",$cre);
		}
		if($save&&$id){
			$data["title"]=$title;
			$data["content"]=$content;
			$data["edit"]=date("Y-m-d H:i:s",time());
			$re = $PG->where("`id`=".$id)->save($data);
			if($re){
				$this->success("修改成功！","/?a=index&m=shopadmin&c=page");
			}else{
				$this->success("修改失败。。。","/?a=index&m=shopadmin&c=page");
			}
			return;
		}
		if($add){
			$data["title"]=$title;
			$data["content"]=$content;
			$data["edit"]=date("Y-m-d H:i:s",time());
			$re = $PG->where("`id`=".$id)->add($data);
			if($re){
				$this->success("创建成功！","/?a=index&m=shopadmin&c=page");
			}else{
				$this->success("创建失败。。。","?/a=index&m=shopadmin&c=page");
			}
			return;
		}
		$this->assign("current","page");
		$this->display();
	}
	public function show(){
		$id =(int)I("get.id");
		$P = M("Page");
		$info["msg"]=htmlspecialchars_decode($P->where("`id`=1")->getField("`content`"));
		$info["type"]="success";
		$show = current($P->where("`id`=".$id)->getField("`id`,`title`,`content`"));
		$show["title"]=htmlspecialchars_decode($show["title"]);
		$show["content"]=htmlspecialchars_decode($show["content"]);
		if($info["msg"]){
		$this->assign("info",$info);
		}
		$this->assign("show",$show);
		$this->display();
	}
	public function task(){
		// 108842915
		$sid = I("sid");
		$tools = C("GFT_TYPE");
		$tools["youhuiq"]=array("name"=>"优惠券","status"=>1,"id"=>2,"tip"=>"填写领取地址");
		$where="`sm_events_gifts`.`sid`=".($sid?$sid:session("SID"));
		$E = D("UsersGifts");//users_gifts
		$E->sid=$this->sid;
		$E->where=$where;
		$E->GetTotal();
		$data = $E->SendList();
		$str = "奖品类型,奖品等级,奖品价值,奖品名称,中奖人,中奖时间,店铺id\n";   
		foreach($data as $k=>$gift){
			$data[$k]["level"]=getlevel($data[$k]["level"]);
			foreach($tools as $tool){
				if($data[$k]["type"]==$tool["id"]){
					$data[$k]["type"]=strip_tags($tool["name"]);
				}
			}
			$str.=$data[$k]["type"].","
				 .strip_tags($data[$k]["level"]).","
				 .$data[$k]["price"].","
				 .$data[$k]["name"].","
				 .$data[$k]["usernick"].","
				 .date("Y-m-d H:i:s",strtotime($data[$k]["actiontime"])).","
				 .$data[$k]["sid"]
				 ."\n";
		}
		$filename = "中奖信息".date('Ymd').'.csv'; //设置文件名   
		header("Content-type:text/csv");   
		header("Content-Disposition:attachment;filename=".$filename);   
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
		header('Expires:0');   
		header('Pragma:public');   
		echo $str;   
		// $T = M("Otask");
		// $re = current($T->where("`status`=0")->order("`id` DESC")->limit("1")->getField("`id`,`sid`,`order_start`,`order_end`,`order_status`,`order_type`,`num_iid`,`payment`,`rate_level`,`start_time`"));
		// $data["start_time"]=date("Y-m-d H:i:s");
		// $T->where("`id`=".$re["id"])->save($data);
		// print_r($re);
		// echo 'test';
	} 

}